home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Python 1.3.3 / Python 133 PPC / Mac / scripts / MkDistr_ui.py < prev    next >
Text File  |  1996-05-19  |  9KB  |  348 lines

  1. #
  2. # MkDistr - User Interface.
  3. #
  4. # Jack Jansen, CWI, August 1995
  5. #
  6. # XXXX To be done (requires mods of FrameWork and toolbox interfaces too):
  7. # - Give dialogs titles (need dlg->win conversion)
  8. # - Place dialogs better (???)
  9. # - <return> as <ok>
  10. # - big box around ok button
  11. # - window-close crashes on reopen (why?)
  12. # - Box around lists (???)
  13. # - Change cursor while busy (need cursor support in Qd)
  14. #
  15. import Res
  16. import Dlg
  17. import Ctl
  18. import List
  19. import Win
  20. import Qd
  21. from FrameWork import *
  22. import EasyDialogs
  23. import macfs
  24.  
  25. # Resource IDs
  26. ID_MAIN = 514
  27. MAIN_LIST=1
  28. MAIN_MKDISTR=2
  29. MAIN_CHECK=3
  30. MAIN_INCLUDE=4
  31. MAIN_EXCLUDE=5
  32.  
  33. ID_INCEXC=515
  34. INCEXC_DELETE=2
  35. INCEXC_CHANGE=3
  36. INCEXC_ADD=4
  37.  
  38. ID_INCLUDE=512
  39. ID_EXCLUDE=513
  40. DLG_OK=1
  41. DLG_CANCEL=2
  42. DLG_FULL=3
  43. DLG_PPCDEV=4
  44. DLG_68K=5
  45. DLG_PPC=6
  46. DLG_BUTTONS=[DLG_FULL, DLG_PPCDEV, DLG_68K, DLG_PPC]
  47. DLG_LETTERS=['S', 'P', 'm', 'p']
  48. DLG_SRCPATH=7
  49. DLG_DSTPATH=8
  50.  
  51. ID_DTYPE=516
  52.  
  53. class EditDialogWindow(DialogWindow):
  54.     """Include/exclude editor (modeless dialog window)"""
  55.     
  56.     def open(self, id, (type, src, dst), callback, cancelrv):
  57.         self.id = id
  58.         if id == ID_INCLUDE:
  59.             title = "Include file dialog"
  60.         else:
  61.             title = "Exclude pattern dialog"
  62.         #self.wid.as_Window().SetWTitle(title)
  63.         self.callback = callback
  64.         self.cancelrv = cancelrv
  65.         DialogWindow.open(self, id)
  66.         tp, h, rect = self.wid.GetDialogItem(DLG_SRCPATH)
  67.         Dlg.SetDialogItemText(h, src)
  68.         if id == ID_INCLUDE:
  69.             tp, h, rect = self.wid.GetDialogItem(DLG_DSTPATH)
  70.             Dlg.SetDialogItemText(h, dst)
  71.         for b in range(len(DLG_BUTTONS)):
  72.             if type == None or DLG_LETTERS[b] in type:
  73.                 self.setbutton(DLG_BUTTONS[b], 1)
  74.  
  75.     def setbutton(self, num, value):
  76.         tp, h, rect = self.wid.GetDialogItem(num)
  77.         h.as_Control().SetControlValue(value)
  78.         
  79.     def getbutton(self, num):
  80.         tp, h, rect = self.wid.GetDialogItem(num)
  81.         return h.as_Control().GetControlValue()
  82.     
  83.     def do_itemhit(self, item, event):
  84.         if item in (DLG_OK, DLG_CANCEL):
  85.             self.done(item)
  86.         elif item in DLG_BUTTONS:
  87.             v = self.getbutton(item)
  88.             self.setbutton(item, (not v))
  89.         # else it is not interesting
  90.         
  91.     def done(self, item):
  92.         if item == DLG_OK:
  93.             distlist = ''
  94.             for i in range(len(DLG_BUTTONS)):
  95.                 if self.getbutton(DLG_BUTTONS[i]):
  96.                     distlist = distlist + DLG_LETTERS[i]
  97.             tp, h, rect = self.wid.GetDialogItem(DLG_SRCPATH)
  98.             src = Dlg.GetDialogItemText(h)
  99.             if self.id == ID_INCLUDE:
  100.                 tp, h, rect = self.wid.GetDialogItem(DLG_DSTPATH)
  101.                 dst = Dlg.GetDialogItemText(h)
  102.                 rv = (distlist, src, dst)
  103.             else:
  104.                 rv = (distlist, src)
  105.         else:
  106.             rv = self.cancelrv
  107.         self.close()
  108.         self.callback((item==DLG_OK), rv)
  109.         
  110. class ListWindow(DialogWindow):
  111.     """A dialog window containing a list as its main item"""
  112.     
  113.     def open(self, id, contents):
  114.         self.id = id
  115.         DialogWindow.open(self, id)
  116.         tp, h, rect = self.wid.GetDialogItem(MAIN_LIST)
  117.         rect2 = rect[0], rect[1], rect[2]-16, rect[3]-16    # Scroll bar space
  118.         self.list = List.LNew(rect2, (0, 0, 1, len(contents)), (0,0), 0, self.wid,
  119.                 0, 1, 1, 1)
  120.         self.setlist(contents)
  121.  
  122.     def setlist(self, contents):
  123.         self.list.LDelRow(0, 0)
  124.         self.list.LSetDrawingMode(0)
  125.         if contents:
  126.             self.list.LAddRow(len(contents), 0)
  127.             for i in range(len(contents)):
  128.                 self.list.LSetCell(contents[i], (0, i))
  129.         self.list.LSetDrawingMode(1)
  130.         self.list.LUpdate(self.wid.GetWindowPort().visRgn)
  131.         
  132.     def additem(self, item):
  133.         where = self.list.LAddRow(1, 0)
  134.         self.list.LSetCell(item, (0, where))
  135.         
  136.     def delgetitem(self, item):
  137.         data = self.list.LGetCell(1000, (0, item))
  138.         self.list.LDelRow(1, item)
  139.         return data
  140.         
  141.     def do_listhit(self, event):
  142.         (what, message, when, where, modifiers) = event
  143.         Qd.SetPort(self.wid)
  144.         where = Qd.GlobalToLocal(where)
  145.         if self.list.LClick(where, modifiers):
  146.             self.do_dclick(self.delgetselection())
  147.         
  148.     def delgetselection(self):
  149.         items = []
  150.         point = (0,0)
  151.         while 1:
  152.             ok, point = self.list.LGetSelect(1, point)
  153.             if not ok:
  154.                 break
  155.             items.append(point[1])
  156.             point = point[0], point[1]+1
  157.         values = []
  158.         items.reverse()
  159.         for i in items:
  160.             values.append(self.delgetitem(i))
  161.         return values
  162.         
  163.     def do_rawupdate(self, window, event):
  164.         Qd.SetPort(window)
  165.         self.list.LUpdate(self.wid.GetWindowPort().visRgn)
  166.         
  167.     def do_close(self):
  168.         self.close()
  169.         
  170.     def close(self):
  171.         del self.list
  172.         DialogWindow.close(self)
  173.         
  174.     def mycb_add(self, ok, item):
  175.         if item:
  176.             self.additem(item[1])
  177.             self.cb_add(item)
  178.         
  179. class MainListWindow(ListWindow):
  180.     """The main window"""
  181.  
  182.     def open(self, id, cb_check, cb_run, cb_add):
  183.         ListWindow.open(self, id, [])
  184.         title = "MkDistr: Unresolved files"
  185.         #self.wid.as_Window().SetWTitle(title)
  186.         self.cb_run = cb_run
  187.         self.cb_check = cb_check
  188.         self.cb_add = cb_add
  189.  
  190.     def do_itemhit(self, item, event):
  191.         if item == MAIN_LIST:
  192.             self.do_listhit(event)
  193.         if item == MAIN_MKDISTR:
  194.             fss, ok = macfs.StandardPutFile('Destination folder:')
  195.             if not ok:
  196.                 return
  197.             self.cb_run(fss.as_pathname())
  198.         if item == MAIN_CHECK:
  199.             list = self.cb_check()
  200.             self.setlist(list)
  201.         if item == MAIN_INCLUDE:
  202.             self.do_dclick(self.delgetselection())
  203.         if item == MAIN_EXCLUDE:
  204.             for i in self.delgetselection():
  205.                 self.cb_add(('', i, ''))
  206.             
  207.     def do_dclick(self, list):
  208.         if not list:
  209.             list = ['']
  210.         for l in list:
  211.             w = EditDialogWindow(self.parent)
  212.             w.open(ID_INCLUDE, (None, l, ''), self.mycb_add, None)
  213.  
  214.     def mycb_add(self, ok, item):
  215.         if item:
  216.             self.cb_add(item)
  217.  
  218. class IncListWindow(ListWindow):
  219.     """An include/exclude window"""
  220.     def open(self, id, editid, contents, cb_add, cb_del, cb_get):
  221.         ListWindow.open(self, id, contents)
  222.         if editid == ID_INCLUDE:
  223.             title = "MkDistr: files to include"
  224.         else:
  225.             title = "MkDistr: patterns to exclude"
  226.         #self.wid.as_Window().SetWTitle(title)
  227.         self.editid = editid
  228.         self.cb_add = cb_add
  229.         self.cb_del = cb_del
  230.         self.cb_get = cb_get
  231.  
  232.     def do_itemhit(self, item, event):
  233.         if item == MAIN_LIST:
  234.             self.do_listhit(event)
  235.         if item == INCEXC_DELETE:
  236.             old = self.delgetselection()
  237.             for i in old:
  238.                 self.cb_del(i)
  239.         if item == INCEXC_CHANGE:
  240.             self.do_dclick(self.delgetselection())
  241.         if item == INCEXC_ADD:
  242.             w = EditDialogWindow(self.parent)
  243.             w.open(self.editid, (None, '', ''), self.mycb_add, None)
  244.             
  245.     def do_dclick(self, list):
  246.         if not list:
  247.             list = ['']
  248.         for l in list:
  249.             old = self.cb_get(l)
  250.             self.cb_del(l)
  251.             w = EditDialogWindow(self.parent)
  252.             w.open(self.editid, old, self.mycb_add, old)
  253.  
  254. class MkDistrUI(Application):
  255.     def __init__(self, main):
  256.         self.main = main
  257.         Application.__init__(self)
  258.         self.mwin = MainListWindow(self)
  259.         self.mwin.open(ID_MAIN, self.main.check, self.main.run, self.main.inc.add)
  260.         self.iwin = None
  261.         self.ewin = None    
  262.         
  263.     def makeusermenus(self):
  264.         self.filemenu = m = Menu(self.menubar, "File")
  265.         self.includeitem = MenuItem(m, "Show Include window", "", self.showinc)
  266.         self.excludeitem = MenuItem(m, "Show Exclude window", "", self.showexc)
  267.         self.saveitem = MenuItem(m, "Save databases", "S", self.save)
  268.         self.quititem = MenuItem(m, "Quit", "Q", self.quit)
  269.         
  270.     def quit(self, *args):
  271.         if self.main.is_modified():
  272.             rv = EasyDialogs.AskYesNoCancel('Database modified. Save?', -1)
  273.             if rv == -1:
  274.                 return
  275.             if rv == 1:
  276.                 self.main.save()
  277.         raise self
  278.         
  279.     def save(self, *args):
  280.         self.main.save()
  281.         
  282.     def showinc(self, *args):
  283.         if self.iwin:
  284.             if self._windows.has_key(self.iwin):
  285.                 self.iwin.close()
  286.             del self.iwin
  287.         self.iwin = IncListWindow(self)
  288.         self.iwin.open(ID_INCEXC, ID_INCLUDE, self.main.inc.getall(), self.main.inc.add,
  289.             self.main.inc.delete, self.main.inc.get)
  290.         
  291.     def showexc(self, *args):
  292.         if self.ewin:
  293.             if self._windows.has_key(self.ewin):
  294.                 self.ewin.close()
  295.             del self.ewin
  296.         self.ewin = IncListWindow(self)
  297.         self.ewin.open(ID_INCEXC, ID_EXCLUDE, self.main.exc.getall(), self.main.exc.add,
  298.             self.main.exc.delete, self.main.exc.get)
  299.  
  300.     def do_about(self, id, item, window, event):
  301.         EasyDialogs.Message("Test the MkDistr user interface.")
  302.         
  303. def GetType():
  304.     """Ask user for distribution type"""
  305.     d = Dlg.GetNewDialog(ID_DTYPE, -1)
  306.     while 1:
  307.         rv = ModalDialog(None)
  308.         if rv >= 1 and rv <= 4:
  309.             return DLG_LETTERS[rv-1]
  310.             
  311. def InitUI():
  312.     """Initialize stuff needed by UI (a resource file)"""
  313.     Res.OpenResFile('MkDistr.rsrc')
  314.  
  315. class _testerhelp:
  316.     def __init__(self, which):
  317.         self.which = which
  318.         
  319.     def get(self):
  320.         return [self.which+'-one', self.which+'-two']
  321.         
  322.     def add(self, value):
  323.         if value:
  324.             print 'ADD', self.which, value
  325.             
  326.     def delete(self, value):
  327.         print 'DEL', self.which, value
  328.         
  329. class _test:
  330.     def __init__(self):
  331.         import sys
  332.         Res.OpenResFile('MkDistr.rsrc')
  333.         self.inc = _testerhelp('include')
  334.         self.exc = _testerhelp('exclude')
  335.         self.ui = MkDistrUI(self)
  336.         self.ui.mainloop()
  337.         sys.exit(1)
  338.         
  339.     def check(self):
  340.         print 'CHECK'
  341.         return ['rv1', 'rv2']
  342.         
  343.     def run(self):
  344.         print 'RUN'
  345.         
  346. if __name__ == '__main__':
  347.     _test()
  348.